<#if invalid?? && invalid>
<div class="alert alert-danger" role="alert">
	<strong>错误！</strong>
	无效的合同信息！
</div>
<#else>
<style>
	.form-control {
		border: 1px solid #fff;
	}
	
	textarea {
		resize: none;
	}
</style>
<form class="contract-work-plan-multipel-form">
	<table class="table table-bordered work-plans">
		<tr>
			<th width="30"></th>
			<th class="text-center" width="105">
				<span class="not-null-star">*</span>
				里程碑
			</th>
			<th class="text-center">
				<span class="not-null-star">*</span>
				工作内容
			</th>
			<th class="text-center" width="110">
				<span class="not-null-star">*</span>
				开始时间
			</th>
			<th class="text-center" width="110">
				<span class="not-null-star">*</span>
				结束时间
			</th>
			<th class="text-center">备注</th>
		</tr>
		<tr class="data-row">
			<input type="hidden" name="contract.id" value="${cid}" />
			<input type="hidden" name="completed" value="false" />
			<input type="hidden" name="header" value="${Session['login_user'].name}">
			<td style="padding-bottom: 0px;" class="text-center">
				<a href="javascript:;" class="icon-remove text-danger remove-row" title="移除" style="margin-left: 2px;"></a>
			</td>
			<td style="padding: 0px;">
				<select style="box-shadow: none;" class="form-control not-empty" name="milestone">
					<#if milestone??> <#list milestone as m>
					<option value="${m}">${m.value}</option>
					</#list> </#if>
				</select>
			</td>
			<td style="padding: 0px;">
				<textarea style="box-shadow: none;" rows="1" class="form-control not-empty" name="work" placeholder="最多300个汉字"></textarea>
			</td>
			<td style="padding: 0px;">
				<input style="box-shadow: none;" type="text" class="form-control not-empty datetimepicker" data-format="yyyy-MM-dd" data-foot="false" name="startDatePoint" value="2016-01-01" placeholder="请选择时间节点" readonly="readonly">
			</td>
			<td style="padding: 0px;">
				<input style="box-shadow: none;" type="text" class="form-control not-empty datetimepicker" data-format="yyyy-MM-dd" data-foot="false" name="datePoint" value="2016-01-01" placeholder="请选择时间节点" readonly="readonly">
			</td>
			<td style="padding: 0px;">
				<textarea style="box-shadow: none;" rows="1" class="form-control" name="remark" placeholder="最多500个汉字"></textarea>
			</td>
		</tr>
		<tr class="plus-row">
			<td class="text-center">
				<a href="javascript:;" class="icon-plus text-primary add-row"></a>
			</td>
			<td colspan="5" class="text-center"></td>
		</tr>
	</table>
	<div class="row">
		<div class="col-md-12 text-right">
			<div class="btn btn-primary btn-sm btn-submit">确定</div>
			&nbsp;
			<div class="btn btn-default btn-sm" onclick="$.suredy.closeModal();">关闭</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	$(function() {
		// 初始化日期控件
		$.suredy.datetimepicker.init();
		
		var template = $('tr.data-row').prop('outerHTML');
		var plus = $('tr.plus-row').prop('outerHTML');
		
		// 文本框控制
		$('table.work-plans').on('focus', 'textarea', function() {
			var $this = $(this);
			
			if ($this.attr('rows') === 5)
				return false;
			
			$('textarea', $this.parents('tr.data-row')).animate({'height': '+=50'});
			//$(this).attr('rows', 5);	
		});
		
		// 文本框控制
		$('table.work-plans').on('blur', 'textarea', function() {
			$('textarea', $(this).parents('tr.data-row')).animate({'height': '-=50'}, 100);
		});
		
		// 删除一行
		$('table.work-plans').on('click', '.remove-row', function() {
			$(this).parents('tr').remove();
		});
		
		// 增加一行
		$('table.work-plans').on('click', '.add-row', function() {
			var table = $('table.work-plans');
			
			$(this).parents('tr').remove();
			
			table.append(template);
			table.append(plus);
			
			$.suredy.datetimepicker.init();
		});
		

		$('form.contract-work-plan-multipel-form .btn-submit').on('click', function() {
			var form = $('form.contract-work-plan-multipel-form');

			// 校验表单
			if (!$.suredy.formChecker(form)) {
				return false;
			}

			var $this = $(this);
			var okBtn = $('form.contract-work-plan-multipel-form .btn-submit');

			if (okBtn.hasClass('disabled'))
				return false;

			okBtn.addClass('disabled');
			
			var data = {};
			
			var trs = $('table.work-plans tr.data-row')
			
			trs.each(function(i) {
				var eles = $('[name]', $(this));
				
				for (var j = 0; j < eles.length; j++) {
					var ele = eles.eq(j);
					var key = 'array[' + i + '].' + ele.attr('name');
					var val = ele.val();
					
					data[key] = val;
				}
			});

			form.ajaxSubmit({
				url : '${request.contextPath}/contract/work-plan-multiple',
				type : 'post',
				data : data,
				dataType : "json",
				success : function(data, textStatus, jqXHR) {
					if (!data || !data.success) {
						alert(data.msg || '记录工期计划信息失败!');
						okBtn.removeClass('disabled');
						return false;
					}

					$.suredy.refreshContent('${cid}', [ 'div.suredy-tabs' ]);
					$.suredy.closeModal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('记录工期计划信息失败!\n' + errorThrown);
					okBtn.removeClass('disabled');
					return false;
				}
			});
		});
	});
</script>
</#if>
